package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IComputeService;

import javax.annotation.Resource;

/**
 * 计算Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-01
 */
@Service
public class ComputeServiceImpl implements IComputeService {
    @Resource
    private ComputeMapper computeMapper;
    @Resource
    private TypeMapper typeMapper;
    @Resource
    private SpecMapper specMapper;
    @Resource
    private BrandMapper brandMapper;
    @Resource
    private CateMapper cateMapper;

    /**
     * 查询计算
     *
     * @param id 计算主键
     * @return 计算
     */
    @Override
    public Compute selectComputeById(Long id) {
        return computeMapper.selectComputeById(id);
    }

    /**
     * 查询计算列表
     *
     * @param compute 计算
     * @return 计算
     */
    @Override
    public List<Compute> selectComputeList(Compute compute) {
        if (compute.getIds() != null && !"".equals(compute.getIds())) {
            compute.getParams().put("ids", compute.getIds().split(","));
        }
        List<Compute> computes = computeMapper.selectComputeList(compute);
        computes.forEach(item -> {
            if (item.getTypeId() != null) {
                Type type = typeMapper.selectTypeById(item.getTypeId());
                if (type != null) {
                    item.setTypeName(type.getName());
                }
            }
            if (item.getSpecId() != null) {
                Spec spec = specMapper.selectSpecById(item.getSpecId());
                if (spec != null) {
                    item.setSpecName(spec.getName());
                }
            }
            if (item.getBrandId() != null) {
                Brand brand = brandMapper.selectBrandById(item.getBrandId());
                if (brand != null) {
                    item.setBrandName(brand.getName());
                }
            }
            if (item.getCateId() != null) {
                Cate cate = cateMapper.selectCateById(item.getCateId());
                if (cate != null) {
                    item.setCateName(cate.getName());
                }
            }
        });
        return computes;
    }

    /**
     * 新增计算
     *
     * @param compute 计算
     * @return 结果
     */
    @Override
    public int insertCompute(Compute compute) {
        compute.setCreateTime(DateUtils.getNowDate());
        return computeMapper.insertCompute(compute);
    }

    /**
     * 修改计算
     *
     * @param compute 计算
     * @return 结果
     */
    @Override
    public int updateCompute(Compute compute) {
        compute.setUpdateTime(DateUtils.getNowDate());
        return computeMapper.updateCompute(compute);
    }

    /**
     * 批量删除计算
     *
     * @param ids 需要删除的计算主键
     * @return 结果
     */
    @Override
    public int deleteComputeByIds(Long[] ids) {
        return computeMapper.deleteComputeByIds(ids);
    }

    /**
     * 删除计算信息
     *
     * @param id 计算主键
     * @return 结果
     */
    @Override
    public int deleteComputeById(Long id) {
        return computeMapper.deleteComputeById(id);
    }
}
